package com.zzyk.main.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.zzyk.main.mapper.DeviceElectricityDayMapper;
import com.zzyk.main.mapper.DeviceMapper;
import com.zzyk.main.model.pojo.DeviceElectricityDay;
import com.zzyk.main.service.DeviceElectricityDayService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

@Service
@Log4j2
public class DeviceElectricityDayServiceImpl implements DeviceElectricityDayService {

    @Resource
    private DeviceElectricityDayMapper tbDeviceElectricityDayMapper;
    @Resource
    private DeviceMapper deviceMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return tbDeviceElectricityDayMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(DeviceElectricityDay record) {
        return tbDeviceElectricityDayMapper.insert(record);
    }

    @Override
    public int insertSelective(DeviceElectricityDay record) {
        return tbDeviceElectricityDayMapper.insertSelective(record);
    }

    @Override
    public DeviceElectricityDay selectByPrimaryKey(Integer id) {
        return tbDeviceElectricityDayMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(DeviceElectricityDay record) {
        return tbDeviceElectricityDayMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(DeviceElectricityDay record) {
        return tbDeviceElectricityDayMapper.updateByPrimaryKey(record);
    }

    @Override
    public HashMap<String, DeviceElectricityDay> findOverviewOfEnergyUse(Integer enterprise, String month, Integer[] deviceList) {
        Date bDate = DateUtil.date();
        int offsetSize=-2;
        if(StringUtils.isNotBlank(month)){
            month+="-01 00:00:00";
            bDate=DateUtil.parseDate(month);
            offsetSize=-1;
        }
        DateTime offsetMonth = DateUtil.offsetMonth(bDate, offsetSize);
        /*上月数据*/
        DeviceElectricityDay lastMonth= tbDeviceElectricityDayMapper.selectOverviewOfEnergyUse(enterprise, month, bDate,deviceList);
        /*上上月数据*/
        DeviceElectricityDay previousMonth = tbDeviceElectricityDayMapper.selectOverviewOfEnergyUse(enterprise, "00:00:00", offsetMonth, deviceList);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.YEAR, -1);
        c.add(Calendar.MONTH, -1);
        Date y = c.getTime();
        String year = format.format(y);
        DateTime lastYear = DateUtil.parse(year);
        DeviceElectricityDay previouslastYear = tbDeviceElectricityDayMapper.selectOverviewOfEnergyUse(enterprise, "00:00:00", lastYear, deviceList);
        HashMap<String, DeviceElectricityDay> map =new HashMap<>(8);
        map.put("lastMonth",lastMonth);
        map.put("previousMonth",previousMonth);
        map.put("lastYear",previouslastYear);
        return map;
    }



}
